# -*- coding:utf-8 -*-
# -1- 对各种计数提交需要计数去重, cache即可, action_uid_id
# -2- 对评论标记删除的东东进行替换
import jiongDao, json, time, os, hashlib
os.sys.path.append('../comment/')
import commentLogic
os.sys.path.append('../../utils/constants/')
from constants import *

class jiongLogic(object):
    "logics of jiong"
    def __init__(self, mysql_conn, memcache_conn):
        self.__dao = jiongDao.jiongDao(mysql_conn)
        self.__mc = memcache_conn

        dict_comment_table = {}
        dict_comment_table['thread_id_keyname'] = 'jiong_id'
        dict_comment_table['thread_table'] = 'jiong'
        dict_comment_table['comment_table'] = 'jiong_comment'
        dict_comment_table['comment_id_table'] = 'jiong_comment_id'
        self.__commentLogic = commentLogic.commentLogic(dict_comment_table, mysql_conn, memcache_conn)

        self.__delete_text = '该评论已被删除'
        self.__mc_expire_time = 10*86400
    def remove(self, delete_item):
        return self.__dao.remove(delete_item)
    def add(self, jiong_item):
        return self.__dao.add(jiong_item)
    def like(self, like_item):
        # like_item has 'jiong_id', 'uip', 'like'
        key = 'like:jiong:%u:%s' % (like_item['jiong_id'], like_item['uip'],)
        md5str = hashlib.md5(key).hexdigest()
        mckey = md5str[:16]
        if not self.__mc.get(mckey):
            self.__mc.add(mckey, int(time.time()), self.__mc_expire_time)
            return self.__dao.like(like_item)
        else:
            ret = {}
            ret['retcode'] = E_DUP_OPERATION
            ret['message'] = 'like duplicate operation table[%s] uip[%s] jid[%u].' %\
                    ('jiong', like_item['uip'], like_item['jiong_id'],)
            return ret
    def hate(self, hate_item):
        # hate_item has 'jiong_id', 'uip', 'hate'
        key = 'hate:jiong:%u:%s' % (hate_item['jiong_id'], hate_item['uip'],)
        md5str = hashlib.md5(key).hexdigest()
        mckey = md5str[:16]
        if not self.__mc.get(mckey):
            self.__mc.add(mckey, int(time.time()), self.__mc_expire_time)
            return self.__dao.hate(hate_item)
        else:
            ret = {}
            ret['retcode'] = E_DUP_OPERATION
            ret['message'] = 'hate duplicate operation table[%s] uip[%s] jid[%u].' %\
                    ('jiong', hate_item['uip'], hate_item['jiong_id'],)
            return ret
    def ding_comment(self, ding_item):
        # ding_item has 'comment_id', 'op_uid', 'ding'
        return self.__commentLogic.ding_comment(ding_item)
    def add_comment(self, comment_item):
        return self.__commentLogic.add_comment(comment_item)
    def del_comment(self, comment_item,):
        return self.__commentLogic.del_comment(comment_item)
    def get_comment(self, thread_id, pageno):
        return self.__commentLogic.get_comment(thread_id, pageno)
    def query_by_uid(self, uid, pageno):
        ret['retcode'] = 0
        ret['message'] = 'OK'
        ret['retbody'] = self.__dao.query_by_time(uid, trunc_time, pageno)
        return ret
    def trunc_by_time(self, timeline, pageno):
        if   timeline == 'week':
            return self.top_week(pageno)
        elif timeline == 'month':
            return self.top_month(pageno)
        elif timeline == 'all':
            return self.top_all(pageno)
        else:
            return self.top_today(pageno)

    def top_today(self, pageno):
        ret = {}
        ret['retcode'] = 0
        ret['message'] = 'OK'
        trunc_time = int(time.time()) - 24*3600
        ret['retbody'] = self.__dao.query_by_time(trunc_time, pageno)
        return ret
    def top_week(self, pageno):
        ret = {}
        ret['retcode'] = 0
        ret['message'] = 'OK'
        trunc_time = int(time.time()) - 7*24*3600
        ret['retbody'] = self.__dao.query_by_time(trunc_time, pageno)
        return ret
    def top_month(self, pageno):
        ret = {}
        ret['retcode'] = 0
        ret['message'] = 'OK'
        trunc_time = int(time.time()) - 30*24*3600
        ret['retbody'] = self.__dao.query_by_time(trunc_time, pageno)
        return ret
    def top_all(self, pageno):
        ret = {}
        ret['retcode'] = 0
        ret['message'] = 'OK'
        trunc_time = 0
        ret['retbody'] = self.__dao.query_by_time(trunc_time, pageno)
        return ret
